package top.guinguo.plugin_05.util;

import com.chenlb.mmseg4j.analysis.MMSegAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.search.highlight.*;
import org.apache.lucene.util.Version;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by guin_guo on 2017/1/4.
 */
public class SearcherUtil {
    public void search01() {
        try {
            IndexSearcher searcher = new IndexSearcher(IndexReader.open(FileIndexUtils.getDirectory()));
            TermQuery query = new TermQuery(new Term("content", "桂楠"));
            TopDocs tds = searcher.search(query, 50);
            for (ScoreDoc sd : tds.scoreDocs) {
                System.out.println(searcher.doc(sd.doc).get("title"));
            }
            searcher.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void searchByHighlighter(String name) {
        Analyzer a = new MMSegAnalyzer("E:\\test\\lucene\\data");
        try {
            IndexSearcher searcher = new IndexSearcher(IndexReader.open(FileIndexUtils.getDirectory()));
            MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35,
                                                                        new String[]{"title","content"}, a);
//            QueryParser parser = new QueryParser(Version.LUCENE_35, "title", a);
            Query query = parser.parse(name);
            TopDocs tds = searcher.search(query, 20);
            for (ScoreDoc sd : tds.scoreDocs) {
                Document doc = searcher.doc(sd.doc);
                String title = doc.get("title");
                title = lighterStr(a, query, title, "title");
                System.out.println(title+"ttttttttttttttttttt");
                String content = new Tika().parseToString(new File(doc.get("path")));
                content = lighterStr(a, query, content, "content");
                System.out.println(content+"cccccccccccccccccc");
                System.out.println("=======================================================");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (TikaException e) {
            e.printStackTrace();
        } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
        }
    }

    public String lighterStr(Analyzer a, Query query, String txt, String field) throws IOException, InvalidTokenOffsetsException {
        String str = null;
        QueryScorer scorer = new QueryScorer(query);
        Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
        Formatter formatter = new SimpleHTMLFormatter("<b>", "</b>");
        Highlighter highlighter = new Highlighter(formatter, scorer);
        highlighter.setTextFragmenter(fragmenter);
        str = highlighter.getBestFragment(a, field, txt);
        if (str == null) {
            return txt;
        }
        return str;
    }

    public void lighter01() {
        String txt = "吞风吻雨葬落日未曾彷徨" +
                "欺山赶海践雪径也未绝望" +
                "拈花把酒偏折煞世人情狂" +
                "凭这两眼与百臂或千手不能防" +
                "天阔阔雪漫漫共谁同航" +
                "这沙滚滚水皱皱笑着浪荡" +
                "贪欢一刻偏教那女儿情长埋葬" +
                "笑你我枉花光心计" +
                "爱竞逐镜花那美丽" +
                "怕幸运会转眼远逝" +
                "为贪嗔喜恶怒着迷" +
                "责你我太贪功恋势" +
                "怪大地众生太美丽";
        try {
            Query query = new QueryParser(Version.LUCENE_35,"f",new MMSegAnalyzer("E:\\test\\lucene\\data")).parse("美丽 落日 拈花");
            QueryScorer scorer = new QueryScorer(query);
            Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
            Formatter formatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>");
            Highlighter highlighter = new Highlighter(formatter, scorer);
            highlighter.setTextFragmenter(fragmenter);
            String str = highlighter.getBestFragment(new MMSegAnalyzer("E:\\test\\lucene\\data"), "f", txt);
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
